﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GISEntity;

namespace SLServiceLibrary
{
    public class WholeAreaValue//求出所有地区所有产业的值
    {
        public decimal GetWholeAreaValue(DateTime Time, string industryTypeName)
        {
            Server dbServer = new Server();
            decimal value = 0;
            decimal Value = 0;//最终返回值
            int industrytypename = 0;
            List<decimal> getwholevalue = new List<decimal>();//查询结果集
            decimal temp = 0;
            // decimal temp = 0;
            IndusryType it = new IndusryType();
            industrytypename = it.GetIndustryName(industryTypeName);
            using (HnMapEntities database = new HnMapEntities(dbServer.GetConnectString()))
            {
                //var queryResult = from first in database.CityIndustries
                //                  where (first.TIME.Equals(time))
                //                  select first;
                //var temp = queryResult.Single() as CityIndustry;
                ////foreach (var s in temp.FIANCEVALUE.ToList())//循环遍历结果集//笨笨笨，把之前的WholeIndustryValue相加即可
                ////{ 

                ////}
                switch (industrytypename)
                {
                    case 1://从AGRICULTURAL查询
                        #region
                        {

                            //////////////////////
                            //////////////////////
                            /////////////////////
                            Console.WriteLine("AGRICULTURAL1");

                            //////////////////////
                            //////////////////////
                            /////////////////////
                            //var queryResult1 = from first in database.AGRICULTURALs
                            //                   where (first.TIME.Equals(Time))
                            //                   select first.AGRVALUE;
                            //foreach (var s in queryResult1.ToList())
                            //{
                            //    temp += (double)s;
                            //}
                            //value =temp;
                            //getwholevalue.Add(value);

                            var queryResult2 = from first in database.AGRICULTURALs
                                               where (first.TIME.Equals(Time))
                                               select first.FARMINGVALUE;
                            foreach (var s in queryResult2.ToList())
                            {
                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);

                            var queryResult3 = from first in database.AGRICULTURALs
                                               where (first.TIME.Equals(Time))
                                               select first.FORESTYVALUE;
                            foreach (var s in queryResult3.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);

                            var queryResult4 = from first in database.AGRICULTURALs
                                               where (first.TIME.Equals(Time))
                                               select first.HUSBANDRYVALUE;
                            foreach (var s in queryResult4.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);

                            var queryResult5 = from first in database.AGRICULTURALs
                                               where (first.TIME.Equals(Time))
                                               select first.FISHERYVALUE;
                            foreach (var s in queryResult5.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);

                            var queryResult6 = from first in database.AGRICULTURALs
                                               where (first.TIME.Equals(Time))
                                               select first.SERVICEVALUE;
                            foreach (var s in queryResult6.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            Console.WriteLine("AGRICULTURAL2");

                        }
                        #endregion
                        break;
                    case 2://从COUNTYINDUSTRY查询
                        #region
                        {

                            //////////////////////
                            //////////////////////
                            /////////////////////
                            Console.WriteLine("COUNTYINDUSTR");

                            //////////////////////
                            //////////////////////
                            /////////////////////
                            //var queryResult1 = from first in database.COUNTYINDUSTRies//对金融业求和
                            //                   where (first.TIME.Equals(time))
                            //                   select first.FIANCEVALUE;
                            //foreach (var s in queryResult1.ToList())
                            //{
                            //    ///查到所有地市的某个行业某年的某个指标值，求和
                            //    ///
                            //    temp += s;//求单个指标的值
                            //}
                            //value = Convert.ToDouble(temp);//queryResult1.Single()
                            //getwholevalue.Add(value);
                            var queryResult2 = from first in database.COUNTYINDUSTRies//对工业求和
                                               where (first.TIME.Equals(Time))
                                               select first.INDUSTRYVALUE;
                            foreach (var s in queryResult2.ToList())
                            {
                                ///查询所有地市的某个行业的某年的指标值，求和
                                ///

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);//添加到结果集中
                            var queryResult3 = from first in database.COUNTYINDUSTRies
                                               where (first.TIME.Equals(Time))
                                               select first.BUILDINGVALUE;
                            foreach (var s in queryResult3.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult4 = from first in database.COUNTYINDUSTRies
                                               where (first.TIME.Equals(Time))
                                               select first.TRANSPORTVALUE;
                            foreach (var s in queryResult4.ToList())
                            {
                                //求单个指标的值

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult5 = from first in database.COUNTYINDUSTRies
                                               where (first.TIME.Equals(Time))
                                               select first.RETAILVALUE;
                            foreach (var s in queryResult5.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult6 = from first in database.COUNTYINDUSTRies
                                               where (first.TIME.Equals(Time))
                                               select first.ESTATEVALUE;
                            foreach (var s in queryResult6.ToList())
                            {


                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult7 = from first in database.COUNTYINDUSTRies
                                               where (first.TIME.Equals(Time))
                                               select first.SERVICEVALUE;
                            foreach (var s in queryResult7.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult8 = from first in database.COUNTYINDUSTRies
                                               where (first.TIME.Equals(Time))
                                               select first.TOURISMVALUE;
                            foreach (var s in queryResult8.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                        }
                        #endregion
                        break;
                    case 3://从INDUSTRY查询
                        #region
                        {

                            //////////////////////
                            //////////////////////
                            /////////////////////
                            Console.WriteLine("INDUSTR");

                            //////////////////////
                            //////////////////////
                            /////////////////////
                            var queryResult1 = from first in database.INDUSTRies//对金融业求和
                                               where (first.TIME.Equals(Time))
                                               select first.FIANCEVALUE;
                            foreach (var s in queryResult1.ToList())
                            {
                                ///查询所有地市的某个行业的某年的指标值，求和
                                ///

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);//添加到结果集中
                            var queryResult2 = from first in database.INDUSTRies//对工业求和
                                               where (first.TIME.Equals(Time))
                                               select first.INDUSTRYVALUE;
                            foreach (var s in queryResult2.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);

                            var queryResult3 = from first in database.INDUSTRies//建筑业
                                               where (first.TIME.Equals(Time))
                                               select first.BUILDINGVALUE;
                            foreach (var s in queryResult3.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult4 = from first in database.INDUSTRies//交通运输业
                                               where (first.TIME.Equals(Time))
                                               select first.TRANSPORTVALUE;
                            foreach (var s in queryResult4.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult5 = from first in database.INDUSTRies//零售业
                                               where (first.TIME.Equals(Time))
                                               select first.RETAILVALUE;
                            foreach (var s in queryResult5.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult6 = from first in database.INDUSTRies//房地产业
                                               where (first.TIME.Equals(Time))
                                               select first.ESTATEVALUE;
                            foreach (var s in queryResult6.ToList())
                            {


                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult7 = from first in database.INDUSTRies//服务业
                                               where (first.TIME.Equals(Time))
                                               select first.SERVICEVALUE;
                            foreach (var s in queryResult7.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);
                            var queryResult8 = from first in database.INDUSTRies//旅游业
                                               where (first.TIME.Equals(Time))
                                               select first.TOURISMVALUE;
                            foreach (var s in queryResult8.ToList())
                            {

                                //temp += (double)s;
                                temp += s;
                            }
                            value = temp;
                            getwholevalue.Add(value);

                        }
                        #endregion
                        break;
                }
            }

            #region
            ///此处这个list有问题，直接用ADD就求和了？
            ///
            foreach (var s in getwholevalue)//遍历结果集，求和
            {
                Value += s;
            }
            #endregion
            //////////////////////
            //////////////////////
            /////////////////////
            Console.WriteLine(value);
            Console.WriteLine(Value);
            //////////////////////
            //////////////////////
            /////////////////////

            return value;

        }
    }
}